package com.example.api.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.GetObjectRequest;
import com.example.api.dao.ResourceDao;
import com.example.api.model.entity.Resource;
import com.example.api.model.support.RespBean;
import com.example.api.properties.LogisticsProperties;
import com.example.api.properties.OssProperties;
import com.example.api.service.FileService;
import com.example.api.utils.AESUtils;
import com.example.api.utils.DateUtils;
import com.example.api.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private ResourceDao resourceDao;

    @Override
    public Integer upload(String module, MultipartFile file, Integer serviceType, Integer objectType) throws Exception {
        String endPoint = AESUtils.decrypt(LogisticsProperties.SECRET_KEY, OssProperties.ENDPOINT);
        String keyId = AESUtils.decrypt(LogisticsProperties.SECRET_KEY, OssProperties.KEY_ID);
        String keySecret = AESUtils.decrypt(LogisticsProperties.SECRET_KEY, OssProperties.KEY_SECRET);
        String bucketName = AESUtils.decrypt(LogisticsProperties.SECRET_KEY, OssProperties.BUCKET_NAME);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret);
        // 判断BUCKET_NAME是否存在
        if (!ossClient.doesBucketExist(bucketName)) {
            ossClient.createBucket(bucketName);
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        }
        if (Objects.isNull(file)) {
            return null;
        }
        String originalFilename = file.getOriginalFilename();
        if (Objects.isNull(originalFilename)) {
            return null;
        }
        InputStream inputStream = file.getInputStream();
        // 文件名：uuid.扩展名
        originalFilename = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        // 文件根路径
        String key = String.join("/", module, DateUtils.dateToString(new Date(), "yyyyMMdd"), originalFilename);
        // 上传文件流
        ossClient.putObject(bucketName, key, inputStream);
        // 关闭OSSClient
        ossClient.shutdown();
        // 文件url地址
        String url = "https://" + bucketName + "." + endPoint + "/" + key;
        Resource resource = new Resource();
        resource.setAid(SecurityUtil.getCurrentAdmin().getId());
        resource.setServiceType(serviceType);
        resource.setObjectType(objectType);
        resource.setOssUrl(url);
        resource.setOssBucketName(key);
        resource.setObjectSize((int) file.getSize());
        resource.setCreateTime(new Date());
        resourceDao.insertSelective(resource);
        return resource.getId();
    }

    @Override
    public RespBean download() {
        String endPoint = AESUtils.decrypt(LogisticsProperties.SECRET_KEY, OssProperties.ENDPOINT);
        String keyId = AESUtils.decrypt(LogisticsProperties.SECRET_KEY, OssProperties.KEY_ID);
        String keySecret = AESUtils.decrypt(LogisticsProperties.SECRET_KEY, OssProperties.KEY_SECRET);
        String bucketName = AESUtils.decrypt(LogisticsProperties.SECRET_KEY, OssProperties.BUCKET_NAME);
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret);
        String objectName = "avatar/demo.jpg";
        String pathName = "D:\\sorapath\\demo.jpg";
        // 下载文件
        ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));
        // 关闭OSSClient
        ossClient.shutdown();
        return RespBean.success("下载成功");
    }

}
